%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This MATLAB code generates all theory based graphs and all results from 
% the main body and Appendix (excluding Online Appendix) of the paper 
% "Process or Candidate: The International Community and the Demand for 
% Electoral Integrity". 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Preliminaries

clear all;

co = [0 0 1;
      0 0.5 0;
      1 0 0;
      0 0.75 0.75;
      0.75 0 0.75;
      0.75 0.75 0;
      0.25 0.25 0.25]; %this gives rgb triplets for standard colors

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Scenario of Election Hegemon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Solving the FOCS using the symbolic toolbx

% Set symbolic parameters
syms pi Gamma Lambda chi b p c ; 

%Formula of government vote share
x_gov = chi + c + (sqrt(b)-p).*(sqrt(b)-c);

%Formula of resulting bias
beta_p = b-sqrt(b).*p;

%Utility of the international power
u_int = Gamma.*pi.*x_gov - Lambda.*beta_p - c.^2 - p.^2;

%2 FOCS

dc_u_int = diff(u_int,c);
dp_u_int = diff(u_int,p);

% Compute Hessian

dcc_u_int = diff(dc_u_int,c)
dcp_u_int = diff(dc_u_int,p)
dpp_u_int = diff(dp_u_int,p)
dpc_u_int = diff(dp_u_int,c) %symmetry (!)

%Solve the FOCS

S = solve(dc_u_int==0,dp_u_int==0,c,p);

%Compute other outcomes
S.x_gov = chi + S.c + (sqrt(b)-S.p).*(sqrt(b)-S.c);
S.beta_p = b-sqrt(b).*S.p;

%Create a Matlab function from the solution of the FOCs and include
%outcomes
eq_electoral_hegemon = matlabFunction(S.c,S.p,S.x_gov,S.beta_p,'Vars', [pi Gamma Lambda chi b]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figure 2: Optimal Choices c* and p* in (Liberal) Hegemony
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% PLOT OF OPTIMA WITH Gamma = 0.75 and Lamba = 0.5 %%%

chi     = .5     %incumbent vote share
b       = .15    %initial bias
Gamma   = .75    %geo-political importance of country
Lambda  = .5     %importance of liberalism

%Create pi_n-Vector for the x-axis of the plots
pi_low  = -.5;   %lowest value on x-axis
pi_high = .5;     %highest value on x axis
n       = 1001;   %how many points are used for computation of y-values
pi_n    = linspace(pi_low,pi_high,n); %grid for pi

%Create values for the first plot
[cstar,pstar,gvtshare,bias] = eq_electoral_hegemon(pi_n, Gamma, Lambda, chi, b);

%Compute utilities for a comparison
u_nonstrategic_baseline = Gamma.*pi_n.*gvtshare-Lambda.*(b-sqrt(b).*(pstar))-cstar.^2-pstar.^2;

%Plot the values of c* and p*
plot(pi_n,cstar,pi_n,pstar,'--')
set(gca,'YLim',[-0.2 0.2]) 
line('XData', [0 0] , 'YData', [-0.2 0.2] , 'LineStyle', ':', 'LineWidth', 1, 'Color','r') %vertical line for pi=0
legend({'$$c^*$$','$$p^*$$'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel({'Optimal Values $$c^*,p^*$$'},'Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure2a.png')

%%% PLOT OF OPTIMA WITH Gamma = 0.5 and Lambda = 0.5 %%%

chi     =.5  %incumbent vote share
b       =.15 %initial bias
Gamma   =.5 %importance of country
Lambda  =.5  %importance of liberalism

%Create values for the first plot
[cstar,pstar,gvtshare,bias] = eq_electoral_hegemon(pi_n, Gamma, Lambda, chi, b);

%Compute utilities for a comparison
u_nonstrategic_lowergamma = Gamma.*pi_n.*gvtshare-Lambda.*(b-sqrt(b).*(pstar))-cstar.^2-pstar.^2;

%Plot the values of c* and p*
plot(pi_n,cstar,pi_n,pstar,'--')
set(gca,'YLim',[-0.2 0.2]) 
line('XData', [0 0] , 'YData', [-0.2 0.2] , 'LineStyle', ':', 'LineWidth', 1, 'Color','r') %vertical line for pi=0
legend({'$$c^*$$','$$p^*$$'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel({'Optimal Values $$c^*,p^*$$'},'Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure2b.png')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figure 3: An Autocracy May Press for Election Integrity
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% PLOT OF OPTIMA WITH Gamma = 0.75 and Lamba = 0 %%%

chi     =.5  %incumbent vote share
b       =.15 %initial bias
Gamma   =.75 %importance of country
Lambda  = 0  %importance of liberalism

%Create values for the first plot
[cstar,pstar,gvtshare,bias] = eq_electoral_hegemon(pi_n, Gamma, Lambda, chi, b);

%Plot the values of c* and p*
plot(pi_n,cstar,pi_n,pstar,'--')
set(gca,'YLim',[-0.2 0.2]) 
line('XData', [0 0] , 'YData', [-0.2 0.2] , 'LineStyle', ':', 'LineWidth', 1, 'Color','r') %vertical line for pi=0
legend({'$$c^*$$','$$p^*$$'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel({'Optimal Values $$c^*,p^*$$'},'Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure3.png')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figure 4: Outcomes of Election Hegemon Scenario
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% COMPARISON PLOT OF VOTESHARE Lambda = 0 and Lamba = 0.5 %%%

chi     =.5  %incumbent vote share
b       =.15 %initial bias
Gamma   =.75 %importance of country
Lambda  =.5  %importance of liberalism

%Create values1
[cstar1,pstar1,gvtshare1,bias1] = eq_electoral_hegemon(pi_n, Gamma, Lambda, chi, b);

chi     =.5  %incumbent vote share
b       =.15 %initial bias
Gamma   =.75 %importance of country
Lambda  =0   %importance of liberalism

%Create values2
[cstar2,pstar2,gvtshare2,bias2] = eq_electoral_hegemon(pi_n, Gamma, Lambda, chi, b);

%Compare government vote shares
plot(pi_n,gvtshare1,'k',pi_n,gvtshare2,'--k')
hold on
plot(-.5,.65,'<','MarkerSize',10,'MarkerEdgeColor',[0.2 0.2 0.2],'MarkerFaceColor',[0.9  0.75 0])
hold off
set(gca,'YLim',[0 1]) 
legend({'$$\Lambda=0.5$$','$$\Lambda=0$$','SQ'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel('Final Vote Share $$f$$','Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure4a.png')

%Compare bias
plot(pi_n,bias1,'k',pi_n,bias2,'--k')
hold on
plot(-.5,.15,'<','MarkerSize',10,'MarkerEdgeColor',[0.2 0.2 0.2],'MarkerFaceColor',[0.9  0.75 0])
hold off
set(gca,'YLim',[0 0.2]) 
legend({'$$\Lambda=0.5$$','$$\Lambda=0$$','SQ'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel('Resulting Bias $$\beta$$','Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure4b.png')

% Solution of Problem with c=0

chi=.5    %incumbent vote share
b =0.15   %initial bias
Gamma=.75 %geo-policical importance of country
Lambda=.5 %importance of liberalism

pstar = sqrt(b)./2.*(Lambda - pi_n.*Gamma);
cstar = ones(1,n).*0;
gvtshare = chi + cstar + (sqrt(b)-pstar).*(sqrt(b)-cstar);
bias = b - sqrt(b).*pstar;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figure 5: Changing the scope of intervention (Liberal Hegemon) - Part 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[cstar_liberal,pstar_liberal,gvtshare_liberal,bias_liberal] = eq_electoral_hegemon(pi_n, Gamma, Lambda, chi, b);

%%% COMPARISON PLOT OF INVESTMENT IN P %%%

h1=plot(pi_n,pstar,'Color',co(2,:))
h2=line(pi_n,pstar_liberal,'LineStyle','- -','Color',co(2,:))
set(gca,'YLim',[-0.2 0.2]) 
line('XData', [0 0] , 'YData', [-0.2 0.2] , 'LineStyle', ':', 'LineWidth', 1, 'Color','r') %vertical line for pi=0
legend({'$$p^*$$ with $$c=0$$','$$p^*$$'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel({'Optimal Values $$c^*$$'},'Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure5a.png')

% Solution of Problem with p=0

chi=.5    %incumbent vote share
b =0.15   %initial bias
Gamma=.75 %geo-policical importance of country
Lambda=.5 %importance of liberalism

cstar = pi_n.*Gamma./2.*(1-sqrt(b));
pstar = ones(1,n).*0;
gvtshare = chi + cstar + (sqrt(b)-pstar).*(sqrt(b)-cstar);
bias = b - sqrt(b).*pstar;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figure 5: Changing the scope of intervention (Liberal Hegemon) - Part 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[cstar_liberal,pstar_liberal,gvtshare_liberal,bias_liberal] = eq_electoral_hegemon(pi_n, Gamma, Lambda, chi, b);

%%% COMPARISON PLOT OF INVESTMENT IN C %%%

plot(pi_n,cstar,pi_n,cstar_liberal,'--b')
set(gca,'YLim',[-0.2 0.2]) 
line('XData', [0 0] , 'YData', [-0.2 0.2] , 'LineStyle', ':', 'LineWidth', 1, 'Color','r') %vertical line for pi=0
legend({'$$c^*$$ with $$p=0$$','$$c^*$$'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel({'Optimal Values $$c^*$$'},'Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure5b.png')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Scenario of Election War
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Solving the FOCS using the symbolic toolbx
% The following part derives the FOCs, and does some rearranging

syms pi Gammaplus Gammaminus Lambdaplus Lambdaminus b pplus pminus cplus cminus chi

voteshare = chi + cplus +cminus + (sqrt(b)-(pplus+pminus))*(sqrt(b)-cplus-cminus)

uplus = Gammaplus*pi*voteshare-Lambdaplus*(b-sqrt(b)*(pplus+pminus))-cplus^2-pplus^2
uminus = -Gammaminus*pi*voteshare-Lambdaminus*(b-sqrt(b)*(pplus+pminus))-cminus^2-pminus^2

%%%%

dpuplus = diff(uplus,pplus)
dcuplus = diff(uplus,cplus)

%%%%

dpuminus = diff(uminus,pminus)
dcuminus = diff(uminus,cminus)


% General Case: Solve the system of 4 equations in 4 variables and save
% solution in Matlab function. Create a system of algebraic equations from 
% first order conditions

S = solve(dpuplus==0,dcuplus==0, dpuminus==0,dcuminus==0,cplus,cminus,pplus,pminus)

equilibrium = matlabFunction([S.cplus S.cminus S.pplus S.pminus],'Vars', {[chi b Gammaplus Lambdaplus Gammaminus Lambdaminus pi]})

% 1 - SYMMETRIC PARAMETRIZATION

n=1001

chi=.5 %incumbent vote share
b =0.15 %initial bias
Gammaplus=.75  %importance of country for '+'-power
Lambdaplus=.5 %importance of liberalism for '+'-power
Gammaminus=.75  %importance of country for '-'-power
Lambdaminus=.5 %importance of liberalism for '-'-power

%generate a vector of input arguments (the parameter vector) for functions
parameters = [chi b Gammaplus Lambdaplus Gammaminus Lambdaminus pi]

%Create an x-axis for plots
pigrid = linspace(-0.5,0.5,n);

% 1 - SYMMETRIC PARAMETRIZATION - SOLUTION

%intialize vectors of optima
cplusstar = ones(1,n);
cminusstar = ones(1,n);
pplusstar = ones(1,n);
pminusstar = ones(1,n);

%Compute equilibrium for each value of pi
for n = 1:n
    pivalue = pigrid(n);
    parameters = [chi b Gammaplus Lambdaplus Gammaminus Lambdaminus pivalue];
    equilibriumvector = equilibrium(parameters)
    cplusstar(n) = equilibriumvector(:,1);
    cminusstar(n) = equilibriumvector(:,2);
    pplusstar(n) = equilibriumvector(:,3);
    pminusstar(n) = equilibriumvector(:,4);
end

%compute other outcomes
voteshare = chi + cplusstar +cminusstar + (sqrt(b)-(pplusstar+pminusstar)).*(sqrt(b)-cplusstar-cminusstar);
expenditures = [cplusstar.^2+pplusstar.^2; cminusstar.^2+pminusstar.^2];

%save for a comparison with the hegemon case

cplusstar_strategic_symmetric = cplusstar;
pplusstar_strategic_symmetric = pplusstar;
gvtshare_strategic_symmetric = voteshare;
expenditure_strategic_symmetric = cplusstar.^2+pplusstar.^2;
expenditure_strategic_symmetric_minuspower = cminusstar.^2+pminusstar.^2;
bias_strategic_symmetric = b-sqrt(b).*(pplusstar+pminusstar);
uplus_strategic_symmetric = Gammaplus.*pigrid.*voteshare-Lambdaplus.*(b-sqrt(b).*(pplusstar+pminusstar))-cplusstar.^2-pplusstar.^2;
uminus_strategic_symmetric = -Gammaminus.*pigrid.*voteshare-Lambdaminus.*(b-sqrt(b).*(pplusstar+pminusstar))-(cminusstar.^2+pminusstar.^2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figure 10: Equilibrium Choices and Outcomes in Liberal Symmetric Election War
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% COMPARISON PLOT OF INVESTMENT c-* and c+* %%%

plot(pigrid,cplusstar,'b',pigrid,cminusstar,'--b')
set(gca,'YLim',[-0.2 0.2])
line('XData', [0 0] , 'YData', [-0.2 0.2] , 'LineStyle', ':', 'LineWidth', 1, 'Color','r') %vertical line for pi=0
legend({'$$c_+^*$$','$$c_-^*$$'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel({'Optimal Values $$c_+^*,c_-^*$$'},'Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure10a.png')

%%% COMPARISON PLOT OF INVESTMENT p-* and p+* %%%

h1=plot(pigrid,pplusstar,'Color',co(2,:))
h2=line(pigrid,pminusstar,'LineStyle','--','Color',co(2,:))
set(gca,'YLim',[-0.2 0.2])
line('XData', [0 0] , 'YData', [-0.2 0.2] , 'LineStyle', ':', 'LineWidth', 1, 'Color','r') %vertical line for pi=0
legend({'$$p_+^*$$','$$p_-^*$$'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel({'Optimal Values $$p_+^*,p_-^*$$'},'Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure10b.png')

%%% COMPARISON PLOT OF VOTE SHARES  - ELECTION WAR VS. HEGEMON CASE %%%

plot(pi_n,gvtshare1,'k',pi_n,gvtshare_strategic_symmetric,'--k')
hold on
plot(-.5,.65,'<','MarkerSize',10,'MarkerEdgeColor',[0.2 0.2 0.2],'MarkerFaceColor',[0.9  0.75 0])
hold off
set(gca,'YLim',[0 1]) 
legend({'Election Hegemon','Election War','SQ'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel('Final Vote Share $$f$$','Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure10c.png')

%%% COMPARISON PLOT OF BIAS  - ELECTION WAR VS. HEGEMON CASE %%%

plot(pi_n,bias1,'k',pi_n,bias_strategic_symmetric,'--k')
hold on
plot(-.5,.15,'<','MarkerSize',10,'MarkerEdgeColor',[0.2 0.2 0.2],'MarkerFaceColor',[0.9  0.75 0])
hold off
set(gca,'YLim',[0 0.2]) 
legend({'Election Hegemon','Election War','SQ'},'Location','northwest','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel('Resulting Bias $$\beta$$','Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure10d.png')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figure 6: Equilibrium Choices in Election War against a Higher-Stakes Power
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% 1 - PARAMETRIZATION ILLIBERAL

chi=.5 %incumbent vote share
b =0.15 %initial bias
Gammaplus=.75  %importance of country for '+'-power
Lambdaplus=.5 %importance of liberalism for '+'-power
Gammaminus=1.0  %importance of country for '-'-power
Lambdaminus=0 %importance of liberalism for '-'-power %%ILLIBERAL%%

%generate a vector of input arguments (the parameter vector) for functions
parameters = [chi b Gammaplus Lambdaplus Gammaminus Lambdaminus pi]

% 1 - SOLUTION ILLIBERAL

%intialize vectors of optima
cplusstar = ones(1,n);
cminusstar = ones(1,n);
pplusstar = ones(1,n);
pminusstar = ones(1,n);

%Compute equilibrium for each value of pi
for n = 1:n
    pivalue = pigrid(n);
    parameters = [chi b Gammaplus Lambdaplus Gammaminus Lambdaminus pivalue];
    equilibriumvector = equilibrium(parameters)
    cplusstar(n) = equilibriumvector(:,1);
    cminusstar(n) = equilibriumvector(:,2);
    pplusstar(n) = equilibriumvector(:,3);
    pminusstar(n) = equilibriumvector(:,4);
end

%Compute other outcomes
voteshare_illiberal = chi + cplusstar +cminusstar + (sqrt(b)-(pplusstar+pminusstar)).*(sqrt(b)-cplusstar-cminusstar);
bias_illiberal = b-sqrt(b)*(pplusstar+pminusstar);
uplus_strategic_illiberal = Gammaplus.*pigrid.*voteshare_illiberal-Lambdaplus.*(b-sqrt(b).*(pplusstar+pminusstar))-cplusstar.^2-pplusstar.^2;
uminus_strategic_illiberal = -Gammaminus.*pigrid.*voteshare-Lambdaminus.*(b-sqrt(b).*(pplusstar+pminusstar))-(cminusstar.^2+pminusstar.^2);

%%% COMPARISON PLOT OF INVESTMENT c-* and c+* %%%

plot(pigrid,cplusstar,'b',pigrid,cminusstar,'--b')
set(gca,'YLim',[-0.2 0.2])
line('XData', [0 0] , 'YData', [-0.2 0.2] , 'LineStyle', ':', 'LineWidth', 1, 'Color','r') %vertical line for pi=0
legend({'$$c_+^*$$','$$c_-^*$$'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel({'Optimal Values $$c_+^*,c_-^*$$'},'Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure6a.png')

%%% COMPARISON PLOT OF INVESTMENT p-* and p+* %%%

h1=plot(pigrid,pplusstar,'Color',co(2,:))
h2=line(pigrid,pminusstar,'LineStyle','--','Color',co(2,:))
set(gca,'YLim',[-0.2 0.2])
line('XData', [0 0] , 'YData', [-0.2 0.2] , 'LineStyle', ':', 'LineWidth', 1, 'Color','r') %vertical line for pi=0
legend({'$$p_+^*$$','$$p_-^*$$'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel({'Optimal Values $$p_+^*,p_-^*$$'},'Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure6b.png')
 
% 1 - PARAMETRIZATION ANTILIBERAL

chi=.5 %incumbent vote share
b =0.15 %initial bias
Gammaplus=.75  %importance of country for '+'-power
Lambdaplus=.5 %importance of liberalism for '+'-power
Gammaminus=1  %importance of country for '-'-power
Lambdaminus=-.5 %importance of liberalism for '-'-power %%ANTILIBERAL%%

%generate a vector of input arguments (the parameter vector) for functions
parameters = [chi b Gammaplus Lambdaplus Gammaminus Lambdaminus pi]

% 1 - SOLUTION ANTILIBERAL

%intialize vectors of optima
cplusstar = ones(1,n);
cminusstar = ones(1,n);
pplusstar = ones(1,n);
pminusstar = ones(1,n);

%Compute equilibrium for each value of pi
for n = 1:n
    pivalue = pigrid(n);
    parameters = [chi b Gammaplus Lambdaplus Gammaminus Lambdaminus pivalue];
    equilibriumvector = equilibrium(parameters)
    cplusstar(n) = equilibriumvector(:,1);
    cminusstar(n) = equilibriumvector(:,2);
    pplusstar(n) = equilibriumvector(:,3);
    pminusstar(n) = equilibriumvector(:,4);
end

%compute other outcomes
voteshare_antiliberal = chi + cplusstar +cminusstar + (sqrt(b)-(pplusstar+pminusstar)).*(sqrt(b)-cplusstar-cminusstar);
bias_antiliberal = b-sqrt(b)*(pplusstar+pminusstar);
uplus_strategic_antiliberal = Gammaplus.*pigrid.*voteshare_antiliberal-Lambdaplus.*(b-sqrt(b).*(pplusstar+pminusstar))-cplusstar.^2-pplusstar.^2;

%%% COMPARISON PLOT OF INVESTMENT c-* and c+* %%%

plot(pigrid,cplusstar,'b',pigrid,cminusstar,'--b')
set(gca,'YLim',[-0.2 0.2])
line('XData', [0 0] , 'YData', [-0.2 0.2] , 'LineStyle', ':', 'LineWidth', 1, 'Color','r') %vertical line for pi=0
legend({'$$c_+^*$$','$$c_-^*$$'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel({'Optimal Values $$c_+^*,c_-^*$$'},'Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure6c.png')

%%% COMPARISON PLOT OF INVESTMENT p-* and p+* %%%

h1=plot(pigrid,pplusstar,'Color',co(2,:))
h2=line(pigrid,pminusstar,'LineStyle','--','Color',co(2,:))
set(gca,'YLim',[-0.2 0.2])
line('XData', [0 0] , 'YData', [-0.2 0.2] , 'LineStyle', ':', 'LineWidth', 1, 'Color','r') %vertical line for pi=0
legend({'$$p_+^*$$','$$p_-^*$$'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel({'Optimal Values $$p_+^*,p_-^*$$'},'Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure6d.png')

%%% COMPARISON PLOT OF VOTE SHARES  - HIGHER STAKES POWER %%%

plot(pi_n,gvtshare1,'k',pi_n,voteshare_illiberal,'-.k',pi_n,voteshare_antiliberal,'--k')
hold on
plot(-.5,.65,'<','MarkerSize',10,'MarkerEdgeColor',[0.2 0.2 0.2],'MarkerFaceColor',[0.9  0.75 0])
hold off
set(gca,'YLim',[0 1]) 
legend({'Election Hegemon','Election War (Aliberal)','Election War (Illiberal)','SQ'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel('Final Vote Share $$f$$','Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure6e.png')

%%% COMPARISON PLOT OF BIAS  - HIGHER STAKES POWER %%%

plot(pi_n,bias1,'k',pi_n,bias_illiberal,'-.k',pi_n,bias_antiliberal,'--k')
hold on
plot(-.5,.15,'<','MarkerSize',10,'MarkerEdgeColor',[0.2 0.2 0.2],'MarkerFaceColor',[0.9  0.75 0])
hold off
set(gca,'YLim',[0 0.2]) 
legend({'Election Hegemon','Election War (Aliberal)','Election War (Illiberal)','SQ'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel('Resulting Bias $$\beta$$','Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure6f.png')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figure 7: Equilibrium Choices in Election War against a Lower-Stakes Power
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% 1 - PARAMETRIZATION ILLIBERAL

chi=.5 %incumbent vote share
b =0.15 %initial bias
Gammaplus=.75  %importance of country for '+'-power
Lambdaplus=.5 %importance of liberalism for '+'-power
Gammaminus=0.5  %importance of country for '-'-power
Lambdaminus=0 %importance of liberalism for '-'-power %%ILLIBERAL%%

%generate a vector of input arguments (the parameter vector) for functions
parameters = [chi b Gammaplus Lambdaplus Gammaminus Lambdaminus pi]

%Create an x-axis for plots
pigrid = linspace(-0.5,0.5,n);

% 1 - SOLUTION ILLIBERAL

%intialize vectors of optima
cplusstar = ones(1,n);
cminusstar = ones(1,n);
pplusstar = ones(1,n);
pminusstar = ones(1,n);

%Compute equilibrium for each value of pi
for n = 1:n
    pivalue = pigrid(n);
    parameters = [chi b Gammaplus Lambdaplus Gammaminus Lambdaminus pivalue];
    equilibriumvector = equilibrium(parameters)
    cplusstar(n) = equilibriumvector(:,1);
    cminusstar(n) = equilibriumvector(:,2);
    pplusstar(n) = equilibriumvector(:,3);
    pminusstar(n) = equilibriumvector(:,4);
end

%compute other outcomes
voteshare_illiberal = chi + cplusstar +cminusstar + (sqrt(b)-(pplusstar+pminusstar)).*(sqrt(b)-cplusstar-cminusstar);
bias_illiberal = b-sqrt(b)*(pplusstar+pminusstar);
uplus_strategic_illiberal = Gammaplus.*pigrid.*voteshare_illiberal-Lambdaplus.*(b-sqrt(b).*(pplusstar+pminusstar))-cplusstar.^2-pplusstar.^2;
uminus_strategic_illiberal = -Gammaminus.*pigrid.*voteshare-Lambdaminus.*(b-sqrt(b).*(pplusstar+pminusstar))-(cminusstar.^2+pminusstar.^2);

% 1 - PLOTS ILLIBERAL

%%% COMPARISON PLOT OF INVESTMENT c-* and c+* %%%

plot(pigrid,cplusstar,'b',pigrid,cminusstar,'--b')
set(gca,'YLim',[-0.2 0.2])
line('XData', [0 0] , 'YData', [-0.2 0.2] , 'LineStyle', ':', 'LineWidth', 1, 'Color','r') %vertical line for pi=0
legend({'$$c_+^*$$','$$c_-^*$$'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel({'Optimal Values $$c_+^*,c_-^*$$'},'Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure7a.png')

%%% COMPARISON PLOT OF INVESTMENT p-* and p+* %%%

h1=plot(pigrid,pplusstar,'Color',co(2,:))
h2=line(pigrid,pminusstar,'LineStyle','--','Color',co(2,:))
set(gca,'YLim',[-0.2 0.2])
line('XData', [0 0] , 'YData', [-0.2 0.2] , 'LineStyle', ':', 'LineWidth', 1, 'Color','r') %vertical line for pi=0
legend({'$$p_+^*$$','$$p_-^*$$'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel({'Optimal Values $$p_+^*,p_-^*$$'},'Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure7b.png')

% 1 - PARAMETRIZATION ANTILIBERAL

chi=.5 %incumbent vote share
b =0.15 %initial bias
Gammaplus=.75  %importance of country for '+'-power
Lambdaplus=.5 %importance of liberalism for '+'-power
Gammaminus=0.5  %importance of country for '-'-power
Lambdaminus=-.5 %importance of liberalism for '-'-power %%ANTILIBERAL%%

%generate a vector of input arguments (the parameter vector) for functions
parameters = [chi b Gammaplus Lambdaplus Gammaminus Lambdaminus pi]

% 1 - SOLUTION ANTILIBERAL

%intialize vectors of optima
cplusstar = ones(1,n);
cminusstar = ones(1,n);
pplusstar = ones(1,n);
pminusstar = ones(1,n);

%Compute equilibrium for each value of pi
for n = 1:n
    pivalue = pigrid(n);
    parameters = [chi b Gammaplus Lambdaplus Gammaminus Lambdaminus pivalue];
    equilibriumvector = equilibrium(parameters)
    cplusstar(n) = equilibriumvector(:,1);
    cminusstar(n) = equilibriumvector(:,2);
    pplusstar(n) = equilibriumvector(:,3);
    pminusstar(n) = equilibriumvector(:,4);
end

%compute other outcomes
voteshare_antiliberal = chi + cplusstar +cminusstar + (sqrt(b)-(pplusstar+pminusstar)).*(sqrt(b)-cplusstar-cminusstar);
bias_antiliberal = b-sqrt(b)*(pplusstar+pminusstar);
expenditures = [cplusstar.^2+pplusstar.^2; cminusstar.^2+pminusstar.^2];
uplus_strategic_antiliberal = Gammaplus.*pigrid.*voteshare_antiliberal-Lambdaplus.*(b-sqrt(b).*(pplusstar+pminusstar))-cplusstar.^2-pplusstar.^2;

% 1 - PLOTS ANTILIBERAL

%%% COMPARISON PLOT OF INVESTMENT c-* and c+* %%%

plot(pigrid,cplusstar,'b',pigrid,cminusstar,'--b')
set(gca,'YLim',[-0.2 0.2])
line('XData', [0 0] , 'YData', [-0.2 0.2] , 'LineStyle', ':', 'LineWidth', 1, 'Color','r') %vertical line for pi=0
legend({'$$c_+^*$$','$$c_-^*$$'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel({'Optimal Values $$c_+^*,c_-^*$$'},'Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure7c.png')

%%% COMPARISON PLOT OF INVESTMENT p-* and p+* %%%

h1=plot(pigrid,pplusstar,'Color',co(2,:))
h2=line(pigrid,pminusstar,'LineStyle','--','Color',co(2,:))
set(gca,'YLim',[-0.2 0.2])
line('XData', [0 0] , 'YData', [-0.2 0.2] , 'LineStyle', ':', 'LineWidth', 1, 'Color','r') %vertical line for pi=0
legend({'$$p_+^*$$','$$p_-^*$$'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel({'Optimal Values $$p_+^*,p_-^*$$'},'Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure7d.png')

%%% COMPARISON PLOT OF VOTE SHARES  - LOWER-STAKES POWER %%%

plot(pi_n,gvtshare1,'k',pi_n,voteshare_illiberal,'-.k',pi_n,voteshare_antiliberal,'--k')
hold on
plot(-.5,.65,'<','MarkerSize',10,'MarkerEdgeColor',[0.2 0.2 0.2],'MarkerFaceColor',[0.9  0.75 0])
hold off
set(gca,'YLim',[0 1]) 
legend({'Election Hegemon','Election War (Aliberal)','Election War (Illiberal)','SQ'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel('Final Vote Share $$f$$','Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure7e.png')

%%% COMPARISON PLOT OF BIAS  - LOWER-STAKES POWER %%%

plot(pi_n,bias1,'k',pi_n,bias_illiberal,'-.k',pi_n,bias_antiliberal,'--k')
hold on
plot(-.5,.15,'<','MarkerSize',10,'MarkerEdgeColor',[0.2 0.2 0.2],'MarkerFaceColor',[0.9  0.75 0])
hold off
set(gca,'YLim',[0 0.2]) 
legend({'Election Hegemon','Election War (Aliberal)','Election War (Illiberal)','SQ'},'Location','southeast','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel('Resulting Bias $$\beta$$','Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure7f.png')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figure 8: Utility of the Foreign Liberal Power
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

chi     =.5  %incumbent vote share
b       =.15 %initial bias
Gamma   =.75 %importance of country
Lambda  =.5  %importance of liberalism

%Create values for the first plot
[cstar,pstar,gvtshare,bias] = eq_electoral_hegemon(pi_n, Gamma, Lambda, chi, b);

%Compute utilities for a comparison
u_nonstrategic = Gamma.*pi_n.*gvtshare-Lambda.*(b-sqrt(b).*(pstar))-cstar.^2-pstar.^2;

%no intervention utility
u_sq = Gamma.*pi_n.*(chi+b) - Lambda.*b;

%Plot the utilities
plot(pi_n,u_nonstrategic-u_sq,'k',pi_n,uplus_strategic_symmetric-u_sq,'--k',pi_n,uplus_strategic_illiberal-u_sq,':k',pi_n,uplus_strategic_antiliberal-u_sq,'-.k')
hold on
plot(-.5,0,'<','MarkerSize',10,'MarkerEdgeColor',[0.8 0.8 0.8],'MarkerFaceColor',[0.9  0.75 0])
hold off
set(gca,'YLim',[-.18 .06]) 
legend({'Liberal Hegemony','War against Liberal ($-$)','War against Aliberal ($-$)','War against Illiberal ($-$)', 'No Intervention'},'Location','south','Interpreter','latex')
xlabel({'Polarization $$\pi$$'},'Interpreter','latex')
ylabel('Relative Utility of $$(+)$$-power','Interpreter','latex')

%Save plot
saveas(gcf,'matlab_main_body/figure8.png')